package com.chinese.converter;

import java.util.Iterator;
import java.util.Map;

public class Opencc {
	Dictionary dictionary;

	public Opencc() {
		this("s2t");
	}

	public Opencc(String conversion) {
		this.dictionary = new Dictionary(conversion);
	}

	public String getDictName() {
		return this.dictionary.getDictName();
	}

	public void setConversion(String conversion) {
		this.dictionary.setConfig(conversion);
	}

	public String convert(String string) {
		if (string.length() == 0) {
			return "";
		}
		StringBuilder stringBuilder = new StringBuilder(string);
		for (Iterator<Map<String, String>> localIterator1 = this.dictionary.getDictChain().iterator(); localIterator1
				.hasNext();) {
			Map<String, String> dictMap = localIterator1.next();
			for (String key : dictMap.keySet()) {
				int fromIndex = 0;
				int pos = stringBuilder.indexOf(key, fromIndex);
				String converted = (String) dictMap.get(key);
				while (pos >= 0) {
					converted = converted.split(" ")[0];
					stringBuilder.replace(pos, pos + key.length(), converted);
					fromIndex = pos + converted.length();
					pos = stringBuilder.indexOf(key, fromIndex);
				}
			}
		}
		return stringBuilder.toString();
	}	
}
